package org.chen.domain;

import com.baomidou.mybatisplus.annotation.*;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.AllArgsConstructor;
import lombok.experimental.SuperBuilder;
import org.springframework.data.annotation.CreatedDate;

import java.io.Serializable;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

@Data
@SuperBuilder
@NoArgsConstructor
@AllArgsConstructor
@TableName("crawler_task")
public class CrawlerTask implements Serializable {
    
    @TableId(type = IdType.ASSIGN_UUID)
    private String taskId;
    
    private String source;
    
    private String sourceUrl;
    
    private Integer targetCount;
    
    private Boolean scheduled;
    
    @CreatedDate
    @TableField(fill = FieldFill.INSERT)
    private LocalDateTime startTime;
    
    @TableField(fill = FieldFill.INSERT_UPDATE)
    private LocalDateTime endTime;
    
    private Long duration;
    
    @TableField(fill = FieldFill.INSERT)
    private String status;
    
    private Integer success;
    
    private Integer failed;
    
    @TableField(exist = false)
    private List<String> errors;

    private String category;        // 体育分类
    private Integer paragraphCount; // 要爬取的段落数
    private String startUrl;

    private String categoryId;    // 分类ID

    private String categoryPath;  // 分类路径

    // 构造方法
    public CrawlerTask(String source, Integer targetCount, Boolean scheduled) {
        this.source = source;
        this.targetCount = targetCount;
        this.scheduled = scheduled;
        this.status = "PENDING";
        this.errors = new ArrayList<>();
    }
    
    // 计算任务耗时
    public void calculateDuration() {
        if (startTime != null && endTime != null) {
            this.duration = java.time.Duration.between(startTime, endTime).toMillis();
        }
    }
}